import Vue from 'vue'
import VueRouter from 'vue-router'

import Recommend from '@/views/Recommend'

Vue.use(VueRouter)

const routes = [{
  path: '/index',
  component: Recommend
}, {
  path: '/',
  redirect: '/index'
},
{
  path: '/silo/women',
  component: () => import('@/views/Women')
},
{
  path: '/silo/men',
  component: () => import('@/views/Men')
},
{
  path: '/silo/cosmetics',
  component: () => import('@/views/Cosmetics')
},
{
  path: '/silo/crossborder',
  component: () => import('@/views/Crossborder')
},
{
  path: '/silo/kids',
  component: () => import('@/views/Kids')
},
{
  path: '/silo/lifestyle',
  component: () => import('@/views/Lifestyle')
},
{
  path: '/productlist',
  component: () => import('@/views/Productlist')
},
{
  name: 'productdetail',
  path: '/productdetail:id',
  component: () => import('@/views/Productdetail')
}, {
  path: '*',
  redirect: '/notfound'
}, {
  path: '/notfound',
  component: () => import('@/views/NotFound')
},
{
  path: '/login/mobile',
  component: () => import('@/views/Login')
},
{
  path: '/upcoming',
  component: () => import('@/views/Upcoming')
}
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login/mobile') {
    if (localStorage.getItem('token')) { // 说明用户已经登录了
    } else {
      next() // 如果用户没有登录直接跳转到登录界面进行用户登录
    }
  } else {
    next()
  }
})

export default router
